package com.leilei.serviceProject.domain.strategy.service;

import com.leilei.serviceProject.domain.strategy.model.entity.RaffleAwardEntity;
import com.leilei.serviceProject.domain.strategy.model.entity.RaffleFactorEntity;
import com.leilei.serviceProject.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yanglei
 * @description 抽奖策略实现类
 * @create 2024/10/24 10:09
 */
@Slf4j
public abstract class AbstractRaffleStrategy implements IRaffleStrategy, IRaffleStock {
    @Override
    public RaffleAwardEntity performRaffle(RaffleFactorEntity raffleFactor) {
        // 1.校验参数
        raffleParamVerification(raffleFactor);
        // 2.责任链抽奖
        Integer awardId = raffleLogicChain(raffleFactor);
        // 3.规则树抽奖
        DefaultTreeFactory.TreeResultData treeResult = raffleLogicTree(raffleFactor.getStrategyId(), raffleFactor.getUserId(), awardId);
        // 4.返回抽奖结果
        return RaffleAwardEntity.builder()
                .award(treeResult.getAwardId())
                .build();
    }

    protected abstract void raffleParamVerification(RaffleFactorEntity raffleFactor);

    protected abstract Integer raffleLogicChain(RaffleFactorEntity raffleFactor);

    protected abstract DefaultTreeFactory.TreeResultData raffleLogicTree(Long strategyId, String userId, Integer awardId);

}
